package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.NoticeDTO;
import com.quanquan.management.request.NoticeRequestBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;
import utils.StringUtil;

/**
 * @ClassName:NoticeService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/5 16:47
 * @Version 1.0
 */
@Service
public class NoticeService {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    public PageDTO<NoticeDTO> list(NoticeRequestBody requestBody) {
        String json = Request.postJson(storeUrl + "square/query_select", requestBody);
        PageDTO<NoticeDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        if (10013 == resCode) {
            pageDTO.setTotalPage(0);
            pageDTO.setPageSize(1);
            pageDTO.setPage(0);
            pageDTO.setTotal((long) 0);
        }
        return pageDTO;
    }

    public boolean addNotice(NoticeRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "square/add_notice", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean update(NoticeRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "square/update_notice", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean delete(NoticeRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "square/delete", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }


    public NoticeDTO findById(String id) {
        NoticeRequestBody requestBody = new NoticeRequestBody();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "square/query_by_id_manage", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        NoticeDTO noticeDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            noticeDTO = data.toJavaObject(NoticeDTO.class);
            System.out.println(noticeDTO);
        }
        return noticeDTO;
    }


    public boolean updateStatus(NoticeRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "square/update_status",requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 != resCode) {
            return false;
        }
        return true;
    }

}
